#include "LogicClientManager.h"
#include "LogicClientLogin.h"
#include "ProtoSockBase.h"
#include <iostream>

using namespace std;

LogicClientManager * LogicClientManager::pclassobj = NULL;

LogicClientManager * LogicClientManager::GetInstance()
{
	if (NULL == pclassobj)
	{
		pclassobj = new LogicClientManager;

		pclassobj->RegProto();
	}

	return pclassobj;
}

int LogicClientManager::RegProto()
{
	//注册协议的处理函数
	_dispatch.Register<Base::LoginResp>(ProtoClientManager::ProcessProto<Base::LoginResp, LogicClientLoginManager>);

	return 0;
}

int LogicClientManager::OnMessage(Message * msg, void *)
{
	 int ret = _dispatch.Dispath(msg, NULL);

	if (ret)
	{
		cerr<<"派发错误"<<endl;
		return ret;
	}

	return 0;
}

int LogicClientManager::BeginLogin()
{
	//先发送登录协议
	Base::LoginReq * loginmsg = new Base::LoginReq  ;

	loginmsg->set_openid("test");
	loginmsg->set_seqid(12);

	int ret = ProtoSockBase::SendProto(fd, loginmsg);

	if (ret)
	{
		cout<<"发送登录协议失败,"<<fd<<endl;
		return ret;
	}

	cout<<"发送登录协议成功,"<<fd<<endl;

	return 0;
}

int LogicClientManager::SendMessage(Message * msg)
{
	return ProtoSockBase::SendProto(fd, msg);
}
